package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.QualityInspetionAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.QualityInspetion;
import com.isunland.managesystem.entity.QualityInspetionListOriginal;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/7 16:04
 */
public class QualityInspetionListFragment extends BaseListFragment {
  public static final String EXTRA_ID =
      QualityInspetionListFragment.class.getSimpleName() + "EXTRA_ID";
  public static final String EXTRA_STATUS =
      QualityInspetionListFragment.class.getSimpleName() + "EXTRA_STATUS";
  private static final int REQUEST_ADD = 1;
  private String mId;
  private ArrayList<QualityInspetion> mList;
  private QualityInspetionAdapter mAdapter;
  private String mStatus;

  public static QualityInspetionListFragment newInstance(String id, String status) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_ID, id);
    bundle.putString(EXTRA_STATUS, status);
    QualityInspetionListFragment fragment = new QualityInspetionListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  public void initView(View view, Bundle savedInstanceState) {
    setTitleCustom(R.string.qualityInspetion);
    setListAdapter(mAdapter);
  }

  public void initData() {
    mId = getArguments().getString(EXTRA_ID);
    mStatus = getArguments().getString(EXTRA_STATUS);
    mList = new ArrayList<>();
    mAdapter = new QualityInspetionAdapter(mActivity, mList);
  }

  @Override public String getUrl() {
    return ApiConst.URL_QUERY_LIST_QUALITYINSPETION;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("mainId", mId);
    params.put("orderField", "REG_DATE");
    params.put("orderSeq", "DESC");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    QualityInspetionListOriginal original =
        new Gson().fromJson(json, QualityInspetionListOriginal.class);
    mList.clear();
    mList.addAll(original.getRows());
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (MyStringUtil.equals(mStatus, DataStatus.NEW) || MyStringUtil.equals(mStatus,
        DataStatus.NEWBACK)) {
      inflater.inflate(R.menu.menu_add, menu);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        QualityInspetionDetailActivity.newInstance(this, mActivity, REQUEST_ADD, mId);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_ADD) volleyPost();
  }
}

